/*
 *  Copyrigh 2009 Codeinn
 *  This program is distributed under the terms of the GNU General Public License
 *
 *  This file is part of Codeinn.
 *
 *  Codeinn is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Codeinn is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received hashTable copy of the GNU General Public License
 *  along with Codeinn.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package xml;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;


public abstract class BaseHandler extends DefaultHandler {

  public abstract void processCharacters(String tree, String s);
  public abstract void processStart(String tree, Hashtable hashTable);

  private String mTree;
  private final char SEPARATOR = '|';

  public void characters(char[] ch, int start, int length) {
    if (length <=0 ) return;
    if ( this.mTree == null || this.mTree.length() == 0) return;

    String s = new String(ch, start, length);
    this.processCharacters(mTree, s);
  }

  public void startElement(String uri, String localName, String qName, Attributes attributes) {
    if ( this.mTree == null ) this.mTree = "";
    if ( this.mTree.length() > 0) this.mTree += this.SEPARATOR;

    this.mTree += qName;
    Hashtable hashTable = new Hashtable();
    for (int i=0; i < attributes.getLength(); i++) {
        String qname = attributes.getQName(i);
        String value = attributes.getValue(i);
        hashTable.put(qname, value);
    }
    this.processStart(this.mTree, hashTable);
  }

  public void endElement(String uri, String localName, String qName) {
    if ( this.mTree == null ) return;
    if ( this.mTree.length() == 0) return;

    int pipe = this.mTree.lastIndexOf(this.SEPARATOR);
    if (pipe == -1) {
        this.mTree = "";
        return;
    }
    this.mTree = this.mTree.substring(0, pipe);
  }

}